package org.emp.controller;

import org.emp.common.Result;
import org.emp.pojo.dto.DeptDTO;

import org.emp.pojo.entity.Dept;
import org.emp.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 加油
 *
 * @author konglingfeng
 * @date 2025/8/11
 * @desc
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/depts")
    public Result<List<Dept>> getDepts() {
        List<Dept> depts = deptService.findAllDept();
        if (depts == null) {
            return Result.error("查询所有部门失败");
        }
        return Result.success("成功查询所有部门",depts);
    }

    @GetMapping("/dept/list")
    public Result<List<Dept>> getDept(@RequestBody DeptDTO searchDeptDTO) {
        System.out.println(searchDeptDTO.getDeptName()+searchDeptDTO.getCode());
        List<Dept> depts = deptService.searchDeptByDeptNameAndCode(searchDeptDTO);
        if (depts == null) {
            return Result.error("查询部门失败");
        }
        return Result.success("成功查询部门",depts);
    }

    @PostMapping("/dept/add")
    public Result addDept(@RequestBody DeptDTO deptDTO) {
        System.out.println("deptDTO = " + deptDTO.getDeptName()+deptDTO.getCode());
        Boolean isSuccess = deptService.insertDept(deptDTO);
        if (isSuccess) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }
}
